package com.example.myFirstWebmagic.airQualityForecasting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.example.myFirstWebmagic.pipeline.ConsolePipelineDemo;
import com.google.gson.Gson;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * 
 * ClassName: AreaForecastProcess 
 * @Description: 全国及重点区域空气质量形势预报
 * @author xiongyao
 * @date 2017年3月13日
 */
public class AreaForecastProcess implements PageProcessor{

	Logger log=LoggerFactory.getLogger(AreaForecastProcess.class);
	
	//获取区域形势预报的ajax的url
	private String apiUrl="http://106.37.208.228:8082/AreaForecast/ChangeArea?areaCode=";
	
	private String homeUrl="http://106.37.208.228:8082/AreaForecast";
	
	//全国及终端区域编码
	private List<String> areaCodes= new ArrayList<String>();
	//全国及终端区域名称
	private List<String> areaNames= new ArrayList<String>();
	
	private Site site = Site.me().setRetryTimes(10).setSleepTime(5000);
	
	private List<EnAreaForecast> areaForecasts=new ArrayList<EnAreaForecast>();
	
	private Map<String, String> areaMap=new HashMap<String, String>();
	
	@Override
	public void process(Page page) {
		// TODO Auto-generated method stub
		
		if(page.getRequest().getUrl().equals(homeUrl)){
			//全国及终端区域编码
			areaCodes= page.getHtml().xpath("//*[@id='areaList']//a/@areacode").all();
			//全国及终端区域名称
			areaNames= page.getHtml().xpath("//*[@id='areaList']//a/text()").all();
			
			List<String> targetUrls=new ArrayList<String>();
			for(String areaCode:areaCodes){
				targetUrls.add(apiUrl+areaCode);
				areaMap.put(areaCode, areaNames.get(areaCodes.indexOf(areaCode)));
			}
			page.addTargetRequests(targetUrls);
		}else{
			Gson gson=new Gson();
			EnAreaForecast areaForecast= gson.fromJson(page.getJson().toString(), EnAreaForecast.class);
			AreaForecastPK id=new AreaForecastPK();
			id.setDate(areaForecast.getForecastPerson());
			id.setAreaName(areaMap.get(areaForecast.getAreaCode()));
			areaForecast.setId(id);
			areaForecasts.add(areaForecast);
			page.putField("objectList", areaForecasts);
		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String [] startUrl=new String[]{"http://106.37.208.228:8082/AreaForecast"};
		// TODO Auto-generated method stub
		Spider.create(new AreaForecastProcess())
        //从"http://datacenter.mep.gov.cn/index!getCityMain.action"开始抓
        .addUrl(startUrl)
        .addPipeline(new ConsolePipelineDemo())
        //开启5个线程抓取
        .thread(5)
        //启动爬虫
        .run();
			
	}
	
	@Override
	public Site getSite() {
		// TODO Auto-generated method stub
		return this.site;
	}

}
